package mark.tom.sail;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;

import java.util.Map;

import static mark.tom.sail.Util.log_e;

public class FastJsonDataConverter implements DataConverter {

    private final LogInterceptor logInterceptor;

    public FastJsonDataConverter(@NonNull LogInterceptor logInterceptor) {
        this.logInterceptor = logInterceptor;
    }

    @Override
    public String convert(@NonNull Map<String, Object> data) {
        String result = null;
        try {
            result = JSON.toJSONString(data);
        } catch (Exception ex) {
            log_e(logInterceptor, ex.getMessage());
        }
        return result;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map<String, Object> convert(@NonNull String data) {
        Map<String, Object> result = null;
        try {
            result = JSON.parseObject(data, Map.class);
        } catch (Exception ex) {
            log_e(logInterceptor, ex.getMessage());
        }
        return result;
    }
}
